package warmachineroster.grapheditor.figure;

import java.net.URL;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.swt.graphics.Image;

import warmachinesystem.provider.WarmachineSystemEditPlugin;

public class BaseRosterElementFigure extends RoundedRectangle {

	private Label nameLabel = new Label();
	private Label fieldAllowanceLabel = new Label();
	private Label pointCostLabel = new Label();

	private Figure figurePanel;

	private String armyListCode;
	
	public BaseRosterElementFigure(String name, String armyListCode) {
		this.armyListCode = armyListCode;
		
		this.setCornerDimensions(new Dimension(5, 5));
		ToolbarLayout layout = new ToolbarLayout();
		layout.setVertical(true);
		layout.setSpacing(5);
		layout.setStretchMinorAxis(true);
		
		setLayoutManager(layout);
		setBorder(new MarginBorder(5));
		setForegroundColor(ColorConstants.black);
		setOpaque(true);
		initFigurePanel();
		configureFigurePanel();
		add(figurePanel);
		getNameLabel().setText(name);
		
	}



	protected void configureFigurePanel() {
		addMainFigure(figurePanel);
		addNameLabel(figurePanel);
		addFieldAllowance(figurePanel);
		addPointCost(figurePanel);
		addAlFigureImg(figurePanel);
		addSpecialRule(figurePanel);
	}

	protected void addAlFigureImg(Figure figurePanel) {
		figurePanel.add(getAlFigure());
	}

	/**
	 * crea una figure che conterrà tutti gli elementi
	 * da visualizzare e imposta il layoutManager
	 */
	private void initFigurePanel() {
		Figure figure = new Figure();
		GridLayout layoutManager = new GridLayout();
		layoutManager.numColumns = 6;
		layoutManager.makeColumnsEqualWidth = false;

		figure.setLayoutManager(layoutManager);

		figure.setBackgroundColor(ColorConstants.tooltipBackground);
		figurePanel = figure;

	}

	protected void addSpecialRule(Figure figure) {
		GridLayout layoutManager = (GridLayout) figure.getLayoutManager();
		RectangleFigure rect = new RectangleFigure();
		GridData gd = new GridData();
		gd.widthHint = 50;
		gd.horizontalSpan = 4;
		gd.grabExcessHorizontalSpace = true;
		layoutManager.setConstraint(rect, gd);
		figure.add(rect);
	}

	protected void addNameLabel(Figure figure) {
		figure.add(nameLabel);
	}

	protected void addMainFigure(Figure figure) {
		GridLayout layoutManager = (GridLayout) figure.getLayoutManager();
		ImageFigure mainImgFigure = getImageFigure();
		GridData gd = new GridData();
		gd.heightHint = 50;
		gd.widthHint = 50;
		gd.verticalSpan = 2;
		layoutManager.setConstraint(mainImgFigure, gd);
		figure.add(mainImgFigure);
	}

	protected void addPointCost(Figure figure) {
		figure.add(new Label("Point Cost"));
		figure.add(pointCostLabel);
	}

	protected void addFieldAllowance(Figure figure) {
		figure.add(new Label("Field Allowance: "));
		figure.add(fieldAllowanceLabel);
	}

	private ImageFigure getImageFigure() {
			String imgKey = "full/obj32/"+ armyListCode + "_32.png";
			Image img = getImage(imgKey);
			return new ImageFigure(img);
	}

	protected Image getImage(String imgKey) {
		URL imgUrl = (URL) getResourceLocator().getImage(imgKey);
		return ExtendedImageRegistry.INSTANCE.getImage(imgUrl);
	}

	private ImageFigure getAlFigure() {
		String imgKey = "full/obj16/" + armyListCode + "_16.png";
		Image img = getImage(imgKey);
		return new ImageFigure(img);
	}

	protected Label getNameLabel() {
		return nameLabel;
	}

	protected Label getFieldAllowanceLabel() {
		return fieldAllowanceLabel;
	}

	protected Label getPointCostLabel() {
		return pointCostLabel;
	}

	protected Figure getFigurePanel() {
		return figurePanel;
	}

	public void setFieldAllowance(String fa) {
		getFieldAllowanceLabel().setText(fa);
	}

	public void setPointCost(int pointCost) {
		getPointCostLabel().setText(String.valueOf(pointCost));
	}

	/**
	 * Return the resource locator for this item provider's resources. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 */
	public ResourceLocator getResourceLocator() {
		return WarmachineSystemEditPlugin.INSTANCE;
	}
}
